package com.xncoding.freemarker.utile;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.net.URL;

public class FileUtil {

    public static void download(String filename, HttpServletResponse res) throws IOException {
        // 发送给客户端的数据
        OutputStream outputStream = res.getOutputStream();
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        // 读取filename
        bis = new BufferedInputStream(new FileInputStream(new File(filename )));
        int i = bis.read(buff);
        while (i != -1) {
            outputStream.write(buff, 0, buff.length);
            outputStream.flush();
            i = bis.read(buff);
        }
    }
    public static void download04(String filename, HttpServletResponse res) throws IOException {
        // 发送给客户端的数据
        OutputStream outputStream = res.getOutputStream();
        byte[] buff = new byte[1024];
        File file = new File(filename);
        BufferedInputStream bis  = new BufferedInputStream(new FileInputStream(new File(filename )));
        int i = bis.read(buff);
        while (i != -1) {
            outputStream.write(buff, 0, buff.length);
            outputStream.flush();
            i = bis.read(buff);
        }
    }
    public static void download03(String filename, HttpServletResponse res) throws IOException {
        // 发送给客户端的数据
        OutputStream outputStream = res.getOutputStream();
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        // 读取filename
        bis = new BufferedInputStream(new FileInputStream(new File(filename )));
        int readLength = 0;
        while (( readLength = bis.read(buff)) != -1) {
            outputStream.write(buff, 0, readLength);
        }
        outputStream.flush();

    }
    public static void download02(String filename, HttpServletResponse res) throws IOException {
        // 发送给客户端的数据
        OutputStream outputStream = res.getOutputStream();
        outputStream.write(readFile(filename,"11"));
        outputStream.flush();

    }
    private static byte[] readFile(String fromPath,String seriaNo) {
        File fromfile = new File(fromPath);
        FileInputStream inputStream = null;
        byte[]  bytes = null;
        try {
            inputStream = new FileInputStream(fromfile);
            if (null != inputStream ) {
                bytes =   toByteArray(inputStream, seriaNo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return  bytes;
    }

    /**
     * InputStream转化为byte[]数组
     * @param input
     * @return
     * @throws IOException
     */
    public static byte[] toByteArray(InputStream input,String seriaNo) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] resByte = null;
        try {
            byte[] buffer = new byte[4096];
            int n = 0;
            BufferedInputStream bufferedInputStream = new BufferedInputStream(input);
            while (-1 != (n = bufferedInputStream.read(buffer))) {
                output.write(buffer, 0, n);
            }
            resByte =output.toByteArray();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (null != output) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return resByte;
    }
}

